import {BaseRenderer} from "./base-renderer";
import {h, resolveComponent} from "vue";
import {cloneDeep} from "lodash";

/**
 * 功能组件渲染器
 */
export class ProgramRenderer extends BaseRenderer {
    render(item: any, propsData: any, emit: any) {
        const {eType, props, events, style, isDisable} = item;
        const Type = resolveComponent(eType);

        // 处理事件
        const normalizedEvents = this.processEvents(events);

        // 渲染组件
        return h(Type, {
            ...cloneDeep(props),
            ...normalizedEvents,
            style: style,
            disabled: typeof isDisable === 'function' ? isDisable() : isDisable,
            onClick: () => {
                if (item.event) {
                    emit(item.event);
                }
            }
        }, () => item.label);
    }
}